package com.example.controller.sys;
import com.example.pojo.Role;
import com.example.service.IRoleService;
import com.example.vo.ResultVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;

import java.sql.Date;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author wsl
 * @since 2022-03-27
 */
@RestController
@RequestMapping("/sys/role")
@Api(tags = "角色管理",value = "")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @ApiOperation(value = "分页获取所有角色信息")
    @GetMapping("/query")
    public PageInfo<Role> getRolePageInfo(
            String roleName,
            String roleNameCh,
            Integer enabled,
            Date start,
            Date end,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return roleService.getRolePageInfo(pageNum,pageSize,roleName, roleNameCh, enabled, start, end);
    }

    @ApiOperation(value = "添加角色信息")
    @PostMapping("/add")
    public ResultVo addRole(@RequestBody Role role) {
        return roleService.insertRole(role);
    }

    @ApiOperation(value = "修改角色信息")
    @PutMapping("/update")
    public ResultVo updateRole(@RequestBody Role role) {
        return roleService.updateRole(role);
    }

    @ApiOperation(value = "删除角色")
    @DeleteMapping("/delete")
    public ResultVo deleteRole(Integer[] ids){
        List<Integer> roles = Arrays.asList(ids);
        if(roles.contains(1)) return ResultVo.error("不能删除系统角色!");
        if(roleService.removeByIds(roles)){
            return ResultVo.ok();
        }
        return ResultVo.error();
    }

    @ApiOperation(value = "获取员工角色分配的角色列表")
    @GetMapping("/emp-roles")
    public ResultVo getAssignRoleList(Integer empId){
        return roleService.getAssignRoleList(empId);
    }

    @ApiOperation(value = "查询数据库是否存在相同名称角色")
    @GetMapping("/has-some-RoleName")
    public Boolean getHasSomeRoleName(Integer id,String roleNameCh){
        return roleService.getHasSomeRoleName(id,roleNameCh);
    }

    @ApiOperation(value = "查询数据库是否存在相同名称编号")
    @GetMapping("/has-some-RoleCode")
    public Boolean getHasSomeRoleCode(Integer id,String roleName){
        return roleService.getHasSomeRoleCode(id,roleName);
    }
}


